﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace KTOP.Core.Formatters
{
    public  class NetlistFormatter
    {
        public void Format(Netlists.Netlist netlist, string name, TextWriter tw)
        {
            var formatter = new TextTableFormatter();
            
            var netTable = netlist.Nets
                .Select((net, netId) => 
                    new[] { string.Format("net #{0}", netId) }
                    .Concat(net.Pins.Select(p => p.ToString()))
                    .ToArray())
                .ToArray();

            var infoTable = new[]
                                {
                                    new [] {"modules", netlist.ModuleCount.ToString()},
                                    new [] {"pins", netlist.Modules.First().PinCount.ToString()},
                                    new [] {"nets", netlist.NetCount.ToString()},
                                    
                                };

            tw.WriteLine(name);
            tw.WriteLine();

            formatter.Format(tw, infoTable, TextTableFormatter.Borders.None);
            tw.WriteLine();

            formatter.Format(tw, netTable, TextTableFormatter.Borders.None);
        }

    }
}
